package com.example.shop.goods.dao;

import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.example.shop.goods.dto.EvaluationBrowseInfo;
import com.example.shop.goods.dto.EvaluationQueryParam;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface  EvaluationBrowseInfoDao {
    public static final String ALL_FIELDS_SQL =
            " e.evaluation_id,e.user_id,e.order_id,e.evaluation_type,e.evaluation_content,e.evaluation_video,e.evaluation_date ";

    @Results(id = "EvaluationBrowseInfoResultMap",value = {
            @Result(column = "evaluation_id",property = "evaluationId"),
            @Result(column = "user_id",property = "evaluationUser",
                    one = @One(select = "com.example.shop.user.dao.UserBriefInfoDao.selectById")),
            @Result(column = "evaluation_type",property = "evaluationType"),
            @Result(column = "evaluation_id",property = "thumbUpUserList",
                    many = @Many(select = "com.example.shop.user.dao.UserAvatarDao.selectByEvaluationId")),
            @Result(column = "order_id",property = "userPurchasedGoods",
                    one = @One(select = "com.example.shop.goods.dao.UserPurchasedGoodsDao.selectByOrderId")),
            @Result(column = "evaluation_id",property = "evaluationPictureList",
                    many = @Many(select = "com.example.shop.mapper.EvaluationPictureMapper.selectPictureById")),
            @Result(column = "evaluation_id",property = "thumbUpNumber",
                    one = @One(select = "com.example.shop.mapper.EvaluationUserThumbUpMapper.count"))
    })
    @Select("select " + ALL_FIELDS_SQL +
            "from evaluation e " +
            "where e.evaluation_id = ${evaluationId}")
    EvaluationBrowseInfo selectById(Long evaluationId);

    @ResultMap("EvaluationBrowseInfoResultMap")
    @Select("select " + ALL_FIELDS_SQL +
            "from evaluation e " +
            "where e.goods_id = ${goodsId}")
    List<EvaluationBrowseInfo> selectByGoodsId(Long goodsId);


    @Results(value = {
            @Result(column = "evaluation_id",property = "evaluationId"),
            @Result(column = "user_id",property = "evaluationUser",
                    one = @One(select = "com.example.shop.user.dao.UserBriefInfoDao.selectById")),
            @Result(column = "evaluation_type",property = "evaluationType"),
            @Result(column = "evaluation_id",property = "thumbUpUserList",
                    many = @Many(select = "com.example.shop.user.dao.UserAvatarDao.selectByEvaluationId")),
            @Result(column = "order_id",property = "userPurchasedGoods",
                    one = @One(resultMap = "com.example.shop.goods.dao.UserPurchasedGoodsDao.UserPurchasedGoodsDaoResultMap")),
            @Result(column = "evaluation_id",property = "evaluationPictureList",
                    many = @Many(select = "com.example.shop.mapper.EvaluationPictureMapper.selectPictureById")),
            @Result(column = "thumb_up_number",property = "thumbUpNumber")
    })
    @Select("<script>" +
            "select " + ALL_FIELDS_SQL + UserPurchasedGoodsDao.ALL_FIELDS_SQL +"," +
                "(select count(reutp.user_id) from r_evaluation_user_thumb_up reutp where  reutp.evaluation_id = e.evaluation_id) thumb_up_number " +
            "from evaluation e join `order` o " +
            "on e.order_id = o.order_id " +
            "where " +
            EvaluationQueryParam.QUERY_SQL +
            "</script>")
    List<EvaluationBrowseInfo> select(@Param(Constants.WRAPPER)EvaluationQueryParam queryParam);



}
